package com.sikao.app.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.ArbitrateSubmitBean;
import com.sikao.app.bean.Bean;
import com.sikao.app.dao.BuyerOrderOperateModel;
import com.sikao.app.dao.SellerOrderOperateModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;

public class ArbitrateActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;

	private EditText etReason;
	private Button btn;
	private BuyerOrderOperateModel buyerOrderOperateModel;
	private SellerOrderOperateModel sellerOrderOperateModel;
	private ArbitrateSubmitBean arbitrateSubmitBean;
	private String type;//buyer 买家申请仲裁 seller 卖家申请仲裁
	private String beanId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_arbitrate);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		type = getIntent().getStringExtra("type");
		beanId = getIntent().getStringExtra("bean");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		etReason = (EditText) findViewById(R.id.et_reason);
		btn = (Button) findViewById(R.id.btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		btn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("发起仲裁申请");
		backIv.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.btn:
				if (etReason.getText().toString().length() == 0) {
					Toast.makeText(this, "请填写仲裁理由", Toast.LENGTH_SHORT).show();
					return;
				}
				if (arbitrateSubmitBean == null) {
					arbitrateSubmitBean = new ArbitrateSubmitBean();
				}
				arbitrateSubmitBean.setOrderId(beanId);
				arbitrateSubmitBean.setAction("arbitration");
				arbitrateSubmitBean.setReason(etReason.getText().toString());
				if (type.equals("buyer")) {
					buyerArbitrate();
				} else {
					sellerArbitrate();
				}
				break;
			default:
				break;
		}
	}

	/**
	 * 买家仲裁
	 */
	private void buyerArbitrate() {
		if (buyerOrderOperateModel == null) {
			buyerOrderOperateModel = new BuyerOrderOperateModel(this, new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null) {
						return;
					}
					if (data.getStatus().equals("1")) {
						Toast.makeText(ArbitrateActivity.this, "申请成功,等待后台审核", Toast.LENGTH_SHORT).show();
						setResult(RESULT_OK, getIntent());
						finish();
					} else {
						Toast.makeText(ArbitrateActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		buyerOrderOperateModel.request(JSONUtil.toJSON(arbitrateSubmitBean), true);
	}

	/**
	 * 卖家仲裁
	 */
	private void sellerArbitrate() {
		if (sellerOrderOperateModel == null) {
			sellerOrderOperateModel = new SellerOrderOperateModel(this, new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null) {
						return;
					}
					if (data.getStatus().equals("1")) {
						Toast.makeText(ArbitrateActivity.this, "申请成功,等待后台审核", Toast.LENGTH_SHORT).show();
						setResult(RESULT_OK, getIntent());
						finish();
					} else {
						Toast.makeText(ArbitrateActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		sellerOrderOperateModel.request(JSONUtil.toJSON(arbitrateSubmitBean), true);
	}
}
